package com.limao.CacheManager;

import java.util.Date;
import java.util.List;

import com.Hazelcast.Cache;
import com.Hazelcast.CacheFactory;
import com.limao.DBDataClass.CrowdOrderDataClass;
import com.limao.DBProvider.CrowdOrderProvider;
import com.limao.Model.LzPluginException;
import com.limao.Model.PublicData;
import com.limao.Utils.OrderPayParm;
import com.limao.cache.BasicModule;


public class CrowdOrderCache  extends BasicModule{

	private CrowdOrderProvider provider;
	private static CrowdOrderCache instance;

	public static CrowdOrderCache getInstance() {
		return instance;
	}

	private Cache<String, CrowdOrderDataClass> Order_ManagerCache;

	public CrowdOrderCache() {
    	super("CrowdOrderCache");
    	Order_ManagerCache = CacheFactory.createCache("crowd_OrderCache");
		initProvider();
	}
    
    @Override
	public void initialize() {
        super.initialize();
        instance=this;
    }
	
    public void ClearObjectCache(String id)
    {
    	CrowdOrderDataClass tmp = Order_ManagerCache.get(id);
    	if(tmp!=null)
    		Order_ManagerCache.remove(tmp.orderid);

    } 
  
	
	//&新鲜宝  增加正式订单
	public OrderPayParm Add_Order(int userid,String[] strIdArr, String[] strNumArr, String[] selectextraArr, int paytype,String address,String shengid,String cityid,int alipayType)	throws LzPluginException {
		return provider.Add_Order(userid,strIdArr, strNumArr, selectextraArr, paytype,address,shengid,cityid,alipayType);
	}
	//再支付之余额支付
	public CrowdOrderDataClass resultRepay_Order_ByUser(CrowdOrderDataClass tmp) throws LzPluginException
	{
		return provider.resultRepay_Order_ByUser(tmp);
	}
	
	//&新鲜宝 确认收货
	public CrowdOrderDataClass Update_sign(int order_id, int user_id) throws LzPluginException {
		CrowdOrderDataClass obj = provider.Update_sign(order_id, user_id);
		if (obj != null) {
			Order_ManagerCache.put(obj.orderid, obj);
		}
		return obj;
	}	

	//&新鲜宝   用户查询我的订单
	public PublicData load_OrderByUser_id(int user_id,int type,int curpage) {
		return provider.load_OrderByUser_id(user_id,type,curpage);
	}

	
	//统计近days天内送vip订单的总金额
	public float Get_OrderVipRmbByUseridDays(int userid,int days) {
		return provider.Get_OrderVipRmbByUseridDays(userid,days);
	}	
	
	//&新鲜宝   获取订单详情
	public CrowdOrderDataClass loadOrderByID(int orderid){
		if(orderid<0) return null;
		
		CrowdOrderDataClass obj = Order_ManagerCache.get(orderid+"");
		
		if(obj == null){
			obj = provider.loadOrderByID(orderid);
			if(obj != null)
				Order_ManagerCache.put(obj.orderid, obj);
		}
		return obj;
	}
	
	//&新鲜宝  获取订单详情
	public CrowdOrderDataClass load_OrderByUser_id(int userid,long ctime)	throws LzPluginException {
		return provider.load_OrderByUser_id(userid, ctime);
	}
	
	//&新鲜宝   再次支付　更新订单支付方式
	public CrowdOrderDataClass updateOrderForPayByID(int orderid,int paytype,int alipayType) throws LzPluginException{
		if(orderid<0) return null;
		
		CrowdOrderDataClass obj=provider.updateOrderForPayByID(orderid,paytype,alipayType); //若成功内部已清缓存
		if(obj != null)
				Order_ManagerCache.put(obj.orderid, obj);
		return obj;
	}
	
	//&新鲜宝   再次支付　更新订单支付方式
	public CrowdOrderDataClass OrderForPayByID_insert_delete(int orderid,int paytype,int alipayType) throws LzPluginException{
		if(orderid<0) return null;
		
		CrowdOrderDataClass obj=provider.OrderForPayByID_insert_delete(orderid,paytype,alipayType); //若成功内部已清缓存
		if(obj != null)
				Order_ManagerCache.put(obj.orderid, obj);
		return obj;
	}
	
	//&新鲜宝  用户订单取消
	public CrowdOrderDataClass cancelOrder(int orderid,int userid) throws LzPluginException{
		if (orderid <= 0)
			return null;
		
		CrowdOrderDataClass obj = Order_ManagerCache.get(orderid + "");

			obj = provider.cancelOrder(orderid,userid);
			if (obj != null)
				Order_ManagerCache.put(obj.orderid, obj);


		return obj;
	}

	//&新鲜宝  订单支付结果更新数据库等 第三方支付
	public boolean Deal_Order(String order_sn, int paytype,boolean iserror,	String errormsg) throws Exception
	{
		return provider.Deal_Order( order_sn,paytype,iserror, errormsg);
		
	}
	//&新鲜宝  订单支付结果　余额支付　更新数据库等
	public boolean Deal_Order(String payid) throws Exception 
	{
		return provider.Deal_Order(payid);
		
	}	
	
	/** 定时任务  0点0分定时执行，每隔24小时执行一次   本操作期间，将清除所有缓存，并使服务处于     禁止用户登陆操作。主要完成 对众筹订单的   状态修改，如：可以早请发货否  是否抵押到期了 等**/
	public void AutoCheckCdOrder(Date startDate)  
	{
		
		provider.AutoCheckCdOrder(startDate);

	}	
	
	public void AutoInVrPreOrder()  
	{
		
		provider.AutoInVrPreOrder();

	}	
	
	public void AutoCheckCdOrder2()  
	{
		
		provider.AutoCheckCdOrder2();

	}
	

//**************************************************************************************************/	
	private void initProvider() {
		if (provider == null) {
			try {
				provider = CrowdOrderProvider.getInstance();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
